package me.monster.smarttraffic.ui.bus;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.monster.smarttraffic.R;
import me.monster.smarttraffic.base.IPresenter;
import me.monster.smarttraffic.base.IView;
import me.monster.smarttraffic.tool.ToastUtils;

/**
 * @description
 * @author: Created jiangjiwei in 2020-02-13 15:17
 */
public abstract class BaseBusFragment<P extends IPresenter> extends Fragment implements IView {

    private static final int HANDLER_ACTION = 100;
    private static final int HANDLER_LOOP = 200;
    public P mPresenter;
    @BindView(R.id.tv_bus_distance_title)
    TextView tvTitle;
    @BindView(R.id.tv_bus_distance_1)
    TextView tvDistance1;
    @BindView(R.id.tv_bus_distance_2)
    TextView tvDistance2;
    @BindView(R.id.tv_bus_env)
    TextView tvBusEnv;
    @BindView(R.id.pb_bus_station)
    ProgressBar progressBar;
    private Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case HANDLER_ACTION:
                    loopFetch();
                    handler.sendEmptyMessage(HANDLER_LOOP);
                    break;
                case HANDLER_LOOP:
                    handler.sendEmptyMessageDelayed(HANDLER_ACTION, 1000);
                    break;
                default:
            }
        }
    };

    public abstract P createPresenter();


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mPresenter = createPresenter();
        mPresenter.attachView(this);
        return inflater.inflate(R.layout.fragment_bus, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);
    }

    @Override
    public void onStart() {
        super.onStart();
        tvTitle.setText(getString(R.string.bus_distance_tittle, getPositionInGroup() + 1));
    }

    public void setEnv(String pm, String temp, String hum, String co) {
        tvBusEnv.setText(getString(R.string.dash_main_road, pm, temp, hum, co));
    }

    public void setBusDistance(int distance1, int distance2) {
        tvDistance1.setText(getString(R.string.bus_distance_1, distance1));
        tvDistance2.setText(getString(R.string.bus_distance_2, distance2));
    }

    public void startLoop() {
        handler.sendEmptyMessage(HANDLER_ACTION);
    }

    public void toast(CharSequence msg) {
        ToastUtils.getInstance().toast(msg);
    }

    public void toast(int resId) {
        toast(getString(resId));
    }

    @Override
    public void onDestroyView() {
        mPresenter.detachView();
        handler.removeMessages(HANDLER_LOOP);
        handler.removeMessages(HANDLER_ACTION);
        super.onDestroyView();
    }

    public void showProgress() {
        progressBar.setVisibility(View.VISIBLE);
    }

    public void hideProgress() {
        progressBar.setVisibility(View.GONE);
    }

    public void handleError(int error) {
        switch (error) {
            case BusPresenter.ERROR_STATION_DISTANCE:
                toast("获取公交位置信息出错");
                break;
            case BusPresenter.ERROR_STATION_ENV:
                toast("获取站台环境信息出错");
                break;
            default:
        }
    }

    abstract int getPositionInGroup();

    abstract void loopFetch();
}
